package com.ruoyi.datastatistics.Agreement;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.datastatistics.domain.createcommand.PresetCommandDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class DeGaoNettyV35 {
    /**
     * 解析日冻结数据
     * @param bit
     * @return
     */
    public static JSONObject GetBufferPacketForAll(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        byte[] type = { 0x61, 0x63 };
        boolean flag = getCsForAllType(bit, 9, type);
        if (flag)
        {
            try
            {
                jo = getDSDateinfoFromByte(bit);
            }
            catch (Exception e)
            {
                return null;
            }
            return jo;
        }
        else
        {
            return null;
        }

    }

    /**
     * 日冻结解析主程序
     * @param bit
     * @return
     */
    public static JSONObject getDSDateinfoFromByte(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        try
        {
            if (bit.length > 41)
            {
                byte[] bit_add = new byte[4];
                System.arraycopy(bit, 5, bit_add, 0, 4);
                String temp = getIntFromByte2(bit_add);//终端地址
                jo.put("add", temp);

                byte[] bit_date = new byte[3];
                System.arraycopy(bit, 10, bit_date, 0, 3);

                //冻结日期改为3个字节
                jo.put("date", getStringBCDFromByteFHigh(bit_date));

                jo.put("type", bit[13]);
                jo.put("type_msg", getTerminalTypeMsg(bit[13]));//终端类型

                bit_date = new byte[6];
                System.arraycopy(bit, 14, bit_date, 0, 6);
                //添加6个字节的当前时间年月日时分秒
                jo.put("clock", getStringBCDFromByte(bit_date));



                byte[] bit_total = new byte[2];
                System.arraycopy(bit, 15 + 6, bit_total, 0, 2);
                int total = Integer.parseInt(getIntFromByte3(bit_total));
                jo.put("total", total);//数据总数组

                byte[] bit_xh = new byte[2];
                System.arraycopy(bit, 17 + 6, bit_xh, 0, 2);
                jo.put("xh", Integer.parseInt(getIntFromByte3(bit_xh)));//序号
                Integer index_num = 19 + 6 + 6;//存储第一组组数的起始位置 添加6bit的当前时间、5B每组时间、1B的密度
                Integer num = 10;//大表每组7个字节，每条数据添加3b的时间，由7改为7+3
                Integer bit_length = 6;//终端状态域
                if (bit[13] == 0x02)
                {//i==0
                    num = 22;//每条数据添加3b的时间之后，由19改为19+3
                    bit_length = 9;//终端状态域 为机械大表
                }
                JSONArray json_temp = new JSONArray();
                while (index_num < bit.length - 4)
                {
                    byte[] bit_total_c = { bit[index_num] };//第一组（该时间点）包含的组数
                    Integer total_c = Integer.parseInt(getIntFromByte3(bit_total_c));
                    byte[] info_ds = new byte[total_c * num];
                    System.arraycopy(bit, index_num + 1, info_ds, 0, info_ds.length);
                    JSONObject joc = new JSONObject();
                    joc.put("density", bit[index_num - 1]);//密度

                    //曲线类数据时标 起始时间
                    bit_date = new byte[5];
                    System.arraycopy(bit, index_num - 6, bit_date, 0, 5);

                    //判断时间是否偏移
                    byte density = bit[index_num - 1];//密度
                    String time = getStringBCDFromByte(bit_date);//起始时间
                    String yMdy = time.substring(0, 8);//年月日时
                    int minute = Integer.parseInt(time.substring(8));//分
                    int remainder = minute % (int)(density);//余数
                    minute = minute - remainder;
                    time = minute+"".length() < 2 ? yMdy + "0" + minute : yMdy + minute;
                    joc.put("time", time);
                    // joc.put("time", bit[index_num - 1]);
                    joc.put("num", total_c);
                    getDSDateinfoFromByte(info_ds, joc, total_c, bit[13]);
                    json_temp.add(joc);
                    index_num = index_num + total_c * num + 7;//7为曲线时标Td_c1长度
                }
                jo.put("info", json_temp);
                jo.put("status", getStatusFromByte(bit[bit.length - bit_length]));
                jo.put("signal1", bit[bit.length - bit_length + 1]);
                jo.put("signal", getSignalFromByte(bit[bit.length - bit_length + 1]));

                //终端电池电压
                byte[] temp_voltage = new byte[2];
                System.arraycopy(bit, bit.length - bit_length + 2, temp_voltage, 0, temp_voltage.length);
                jo.put("voltage", getPressureVFromByte(temp_voltage));

                if (bit[13] == 0x02)
                {
                    //流量计电池电压域 类型  01：电量  02：电压 单位：%  范围：0-99
                    byte ElectromagneticPressureType = bit[bit.length - bit_length + 4];
                    jo.put("ElectromagneticPressureType", ElectromagneticPressureType);
                    temp_voltage = new byte[2];
                    System.arraycopy(bit, bit.length - bit_length + 5, temp_voltage, 0, temp_voltage.length);
                    jo.put("ElectromagneticPressure", getPressureVFromByte(temp_voltage));
                }

            }

        }
        catch (Exception e)
        {
            log.error(e.getMessage());
        }
        return jo;
    }

    private static JSONObject getDSDateinfoFromByte(byte[] bit, JSONObject joc, int total, byte type)
    {
        try
        {
            JSONArray json = new JSONArray();
            if (type == 0x01)
            {
                Integer num = 10;
                for (int j = 0; j < total; j++)
                {
                    byte[] temp_dsinfo = new byte[num];//读数+压力+验证 4+ 2+1->2(时间)+4+2+1
                    System.arraycopy(bit, j * num, temp_dsinfo, 0, num);
                    if (isOkTotalForByte(temp_dsinfo, 0))//验证校验位
                    {
                        JSONObject joc_2 = new JSONObject();

                        byte[] temp_t = new byte[3];
                        System.arraycopy(temp_dsinfo, 0, temp_t, 0, 3);
                        //每条数据带的日期 月+日->年 月 日 2-》3
                        joc_2.put("detailsTime", getStringBCDFromByteFHigh(temp_t));


                        byte[] temp_ds = new byte[4];
                        System.arraycopy(temp_dsinfo, 3, temp_ds, 0, temp_ds.length);
                        Long v = getDSFromByte(temp_ds);
                        joc_2.put("TotalFlow", v);

                        byte[] temp_pressure = new byte[2];
                        System.arraycopy(temp_dsinfo, 7, temp_pressure, 0, temp_pressure.length);
                        String p = getPressureVFromByte(temp_pressure);
                        joc_2.put("Pressure", p);
                        json.add(joc_2);
                    }
                }
            }
            if (type == 0x02)
            {
                Integer num = 22;
                for (int j = 0; j < total; j++)
                {
                    byte[] temp_dsinfo = new byte[num];//表正向流量、反向流量、累计流量、瞬时流速+压力+验证
                    System.arraycopy(bit, j * num, temp_dsinfo, 0, num);
                    JSONObject joc_2 = new JSONObject();
                    try {
                        if (isOkTotalForByte(temp_dsinfo, 0))//验证校验位
                        {
                            try
                            {
                                byte[] temp = new byte[3];//时间，下面都偏移3b
                                System.arraycopy(temp_dsinfo, 0, temp, 0, 3);
                                //每条数据带的日期 月+日
                                joc_2.put("detailsTime", getStringBCDFromByteFHigh(temp));
                                //流量计
                                temp = new byte[4];
                                System.arraycopy(temp_dsinfo, 3, temp, 0, temp.length);
                                float v = getFloatFromByte(temp);
                                joc_2.put("ReadNumber", v);//正向流量

                                temp = new byte[4];
                                System.arraycopy(temp_dsinfo, 7, temp, 0, temp.length);
                                v = getFloatFromByte(temp);
                                joc_2.put("ReverseNumber", v);//反向流量

                                temp = new byte[4];
                                System.arraycopy(temp_dsinfo, 11, temp, 0, 4);
                                v = getFloatFromByte(temp);
                                joc_2.put("TotalFlow", v);//累计流量

                                temp = new byte[4];
                                System.arraycopy(temp_dsinfo, 15, temp, 0, 4);
                                v = getFloatFromByte(temp);
                                joc_2.put("InstantNumber", v);//瞬时流量
                            }
                            catch (Exception e)
                            {

                            }


                            byte[] temp_pressure = new byte[2];
                            System.arraycopy(temp_dsinfo, 19, temp_pressure, 0, 2);
                            String p = getPressureVFromByte(temp_pressure);
                            joc_2.put("Pressure", p);
                            json.add(joc_2);

                        }else
                        {
                            //解析数据中验证不通过也加上数据结构体 20191203 kyx
                            joc_2.put("detailsTime", "");
                            json.add(joc_2);
                        }

                    } catch (Exception e)
                    {

                    }


                }
            }
            joc.put("dsinfo", json);
        }
        catch (Exception e)
        {

        }



        return joc;
    }

    /**
     * 报警主程序
     * @param bit
     * @return
     */
    public static JSONObject GetBufferPacketForWarning(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        boolean flag = getCs(bit, 9, (byte) 0x65, "0");
        if (flag)
        {
            try
            {
                Integer templength = bit.length - 12;
                byte[] bit_ = new byte[templength];
                System.arraycopy(bit, 10, bit_, 0, templength);

                jo = getwarningInfoFromByte(bit_);
            }
            catch (Exception e)
            {
                return null;
            }
            return jo;
        }
        else
        {
            return null;
        }

    }

    /**
     * 解析报警
     * @param bit
     * @return
     */
    public static JSONObject getwarningInfoFromByte(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        if (bit.length > 2)
        {
            jo.put("atcAlarm", bit[0]);
            String atcAlarm_str = (int)bit[0] == 0 ? "正常" : "空管报警";
            jo.put("atcAlarm_str", atcAlarm_str);

            jo.put("excitationAlarm", bit[1]);
            String excitationAlarm_str = (int)bit[1] == 0 ? "正常" : "励磁报警";
            jo.put("excitationAlarm_str", excitationAlarm_str);

            try {
                jo.put("flowUpperAlarm", bit[2]);
                String flowUpperAlarm_str = (int)bit[2] == 0 ? "正常" : "流量上限报警";
                jo.put("flowUpperAlarm_str", flowUpperAlarm_str);

                jo.put("flowLowerAlarm", bit[3]);
                String flowLowerAlarm_str = (int)bit[3] == 0 ? "正常" : "流量下限报警";
                jo.put("flowLowerAlarm_str", flowLowerAlarm_str);

                jo.put("pressureUpperAlarm", bit[4]);
                String pressureUpperAlarm_str = (int)bit[4] == 0 ? "正常" : "压力上限报警";
                jo.put("pressureUpperAlarm_str", pressureUpperAlarm_str);

                jo.put("pressureLowerAlarm", bit[5]);
                String pressureLowerAlarm_str = (int)bit[5] == 0 ? "正常" : "压力下限报警";
                jo.put("pressureLowerAlarm_str", pressureLowerAlarm_str);
            } catch (Exception e) {
            }

        }
        return jo;
    }

    /**
     * 定位主程序
     * @param bit
     * @return
     */
    public static JSONObject GetBufferPacketForGPS(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        boolean flag = getCs(bit, 9, (byte) 0x66, "0");
        if (flag)
        {
            try
            {
                Integer templength = bit.length - 12;
                byte[] bit_ = new byte[templength];
                System.arraycopy(bit, 10, bit_, 0, templength);

                jo = getGPSInfoFromByte(bit_);
            }
            catch (Exception e)
            {
                return null;
            }
            return jo;
        }
        else
        {
            return null;
        }

    }

    /**
     * 解析定位信息
     * @param bit
     * @return
     */
    public static JSONObject getGPSInfoFromByte(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        try
        {
            if (bit.length == 31)
            {
                byte[] bit_date = new byte[6];
                System.arraycopy(bit, 0, bit_date, 0, 6);
                jo.put("locationTime", getStringBCDFromByteFHigh(bit_date));  //定位时间

                byte[] bit_lat = new byte[9];
                System.arraycopy(bit, 6, bit_lat, 0, 9);
                String string_lat = new String(bit_lat);//纬度
                jo.put("lat",string_lat);

                byte[] bit_sn = new byte[1];
                System.arraycopy(bit, 15, bit_sn, 0, 1);
                String string_sn = new String(bit_sn);//南北半球指示器
                jo.put("sn", string_sn);

                byte[] bit_lng = new byte[10];
                System.arraycopy(bit, 16, bit_lng, 0, 10);
                String string_lng = new String(bit_lng);//经度
                jo.put("lng", string_lng);

                byte[] bit_ew = new byte[1];
                System.arraycopy(bit, 26, bit_ew, 0, 1);
                String string_ew = new String(bit_ew);//东西半球指示器
                jo.put("ew", string_ew);

                byte[] bit_seahigh = new byte[4];
                System.arraycopy(bit, 27, bit_seahigh, 0, 4);
                jo.put("altitude", getFloatFromByte(bit_seahigh));  //海拔高度
            }
        }
        catch (Exception e)
        {

        }
        return jo;
    }

    /**
     * 日冻结主程序
     * @param bit
     * @return
     */
    public static JSONObject GetBufferPacketForFreezeDay(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        boolean flag = getCs(bit, 9, (byte) 0x67, "0");
        if (flag)
        {
            try
            {
                Integer templength = bit.length - 12;
                byte[] bit_ = new byte[templength];
                System.arraycopy(bit, 10, bit_, 0, templength);

                jo = getFreezeDayInfoFromByte(bit_);
            }
            catch (Exception e)
            {
                return null;
            }
            return jo;
        }
        else
        {
            return null;
        }

    }

    /**
     *解析日冻结
     * @param bit
     * @return
     */
    public static JSONObject getFreezeDayInfoFromByte(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        try
        {
            if (bit.length == 15)
            {
                byte[] bit_date = new byte[3];
                System.arraycopy(bit, 0, bit_date, 0, 3);
                jo.put("freezeDay", getStringBCDFromByteFHigh(bit_date));  //日冻结日期

                byte[] bit_reDn = new byte[4];
                System.arraycopy(bit, 3, bit_reDn, 0, 4);
                jo.put("readDosageN", getFloatFromByte(bit_reDn));

                byte[] bit_rdn = new byte[4];
                System.arraycopy(bit, 7, bit_rdn, 0, 4);
                jo.put("reverseDosageN", getFloatFromByte(bit_rdn));

                byte[] bit_dn = new byte[4];
                System.arraycopy(bit, 11, bit_dn, 0, 4);
                jo.put("dosageN", getFloatFromByte(bit_dn));
            }
        }
        catch (Exception e)
        {
        }
        return jo;
    }

    /**
     * 验证
     *
     * @param bit
     * @param index
     * @param type
     * @return
     */
    public static boolean getCsForAllType(byte[] bit, int index, byte[] type) {
        boolean flag = true;
        if (bit != null && bit.length > 0) {
            int recvlen = Integer.parseInt(Integer.toHexString(bit[1] & 0xFF));
            int recvlen2 = Integer.parseInt(Integer.toHexString(bit[2] & 0xFF));
            recvlen = recvlen2 * 100 + recvlen;
            if (type != null && type.length > 0) {
                boolean temp = false;
                for (byte b : type) {
                    if (bit[index] == b) {
                        temp = true;
                        break;
                    }
                }
                flag = temp;
            }
            if (flag) {
                if (bit[0] != 104) {
                    flag = false;
                } else if (recvlen != bit.length) {
                    flag = false;
                } else if (bit[(bit.length - 1)] != 22) {
                    flag = false;
                } else if (bit[3] != 114) {
                    flag = false;
                } else {
                    byte[] temp1 = new byte[bit.length - 2];
                    System.arraycopy(bit, 0, temp1, 0, temp1.length);
                    byte temp = getTotalForByte(temp1);
                    if (temp != bit[(bit.length - 2)]) {
                        flag = false;
                    }
                }
            }

        }

        return flag;
    }

    /**
     * 验证
     * @param bit
     * @param index
     * @param type
     * @param iset
     * @return
     */
    public static boolean getCs(byte[] bit, int index, byte type, String iset)
    {
        boolean flag = true;
        if (bit != null & bit.length > 0)
        {
            int recvlen = Integer.parseInt(Integer.toHexString(bit[1]& 0xFF));
            int recvlen2 = Integer.parseInt(Integer.toHexString(bit[2] & 0xFF));
            recvlen = recvlen2 * 100 + recvlen;
            if (bit.length <= 0)
            {
                flag = false;
            }
            else if (bit[0] != 104)
            {
                flag = false;
            }
            else if (recvlen != bit.length)
            {
                flag = false;
            }
            else if (bit[(bit.length - 1)] != 22)
            {
                flag = false;
            }
            else if (bit[index] != type)
            {
                flag = false;
            }
            else if (bit[3] != 114)
            {
                flag = false;
            }
            else if (bit[(bit.length - 3)] == 116)
            {
                if (!"".equals(iset) && "1".equals(iset))
                {
                    flag = false;
                }
            }
            else
            {
                byte[] temp1 = new byte[bit.length - 2];
                System.arraycopy(bit, 0, temp1, 0, temp1.length);
                byte temp = getTotalForByte(temp1);
                if (temp != bit[(bit.length - 2)])
                {
                    flag = false;
                }
            }
        }

        return flag;
    }

    /**
     * 验证和
     * @param bit
     * @return
     */
    private static byte getTotalForByte(byte[] bit)
    {
        int sum = 0;
        if (bit.length > 0)
        {
            for (int i = 0; i < bit.length; i++)
            {
                sum = sum + bit[i];
                sum = sum & 0xFF;
            }
        }
        byte rein = (byte)sum;
        return rein;
    }

    /**
     * 两位不足 补零,高位在hou,eg->0x0c 0x02:0c02
     * @param bit
     * @return
     */
    public static String getIntFromByte2(byte[] bit)
    {
        String str = "";
        for (int i = 0; i < bit.length; i++)
        {
            String temp = Integer.toHexString(bit[i] & 0xFF);
            temp = temp.length() < 2 ? ("0" + temp) : temp;
            str = str + temp;
        }
        return str;
    }



    /**
     * 表类型
     * @param type
     * @return
     */
    private static String getTerminalTypeMsg(byte type)
    {
        String str = "";
        if (type == 0x01)
        {
            str = "机械大水表";
        }
        else if (type == 0x02)
        {
            str = "电磁水表";
        }
        return str;
    }

    /**
     * BCD数组转字符
     * @param bit
     * @return
     */
    public static  String getStringBCDFromByte(byte[] bit)
    {
        String str = "";
        if (bit.length > 0)
        {
            for (int i = bit.length - 1; i >= 0; i--)
            {
                String hex = Integer.toHexString(bit[i]& 0xFF);
                if (hex.length()< 2)
                {
                    hex = "0" + hex;
                }
                str = str + hex;
            }
        }
        return str;
    }

    /**
     * 两位不足 补零,高位在qian,eg->0x0c 0x02:0212
     * @param bit
     * @return
     */
    private static String getIntFromByte3(byte[] bit)
    {
        String str = "";
        for (int i = 0; i < bit.length; i++)
        {
            String temp = bit[i] + "";
            temp = temp.length() < 2 ? ("0" + temp) : temp;
            str = temp + str;
        }
        return str;
    }


    /**
     * 状态
     * @param status
     * @return
     */
    public static String getStatusFromByte(int status)
    {
        String statustemp = Integer.toBinaryString(status);//十进制数转换为2进制
        Integer templength = statustemp.length();
        for (int i = 0; i < 8 - templength; i++)
        {
            statustemp = "0" + statustemp;
        }
        int temp1 = Integer.parseInt(statustemp.substring(0, 2),2);
        String str = "";
        switch (temp1)
        {
            case 0:
                str = "状态不明";
                break;
            case 1:
                str = "半阀状态";
                break;
            case 2:
                str = "关阀状态";
                break;
            case 3:
                str = "开阀状态";
                break;
        }

        if (statustemp.substring(4, 5) == "0")
        {
            str = str + ";" + "模块数据类型：光电数据";
        }
        else
        {
            str = str + ";" + "模块数据类型：脉冲计数数据";
        }
        if (statustemp.substring(5, 6) == "0")
        {
            str = str + ";" + "组网正常";
        }
        else
        {
            str = str + ";" + "组网受干扰";
        }

        return str;
    }

    /**
     * 验证和
     * @param bit
     * @param isxh
     * @return
     */
    private static boolean isOkTotalForByte(byte[] bit, Integer isxh)
    {
        boolean re = false;
        byte[] temp = new byte[bit.length - 1];
        System.arraycopy(bit, 0, temp, 0, temp.length);
        if (bit[bit.length - isxh - 1] == getTotalForByte(temp))
        {
            re = true;
        }
        return re;
    }

    public static String getStringBCDFromByteFHigh(byte[] bit)
    {
        String str = "";
        if (bit.length > 0)
        {
            for (int i = 0; i < bit.length; i++)
            {
                String hex = Integer.toHexString(bit[i]& 0xFF);
                if (hex.length() < 2)
                {
                    hex = "0" + hex;
                }
                str = str + hex;
            }
        }
        return str;
    }

    /**
     * 机械大表读数
     * @param bit
     * @return
     */
    public static long getDSFromByte(byte[] bit)
    {
        String str = getIntFromByte2(bit);
        long temp = Long.parseLong(str, 16);
        return temp;
    }

    //解析压力域
    public static String getPressureVFromByte(byte[] bit)
    {
        String str = "";
        if (bit.length == 2)
        {
            str = Integer.toHexString(bit[0]& 0xFF) + "." + (Integer.toHexString(bit[1]& 0xFF).length() < 2 ? "0" + Integer.toHexString(bit[1]& 0xFF) : Integer.toHexString(bit[1]& 0xFF));
        }
        return str;
    }

    /**
     * IEEE754   单精度浮点数  转 十进制
     * @param bytes
     * @return
     */
    public static float getFloatFromByte(byte[] bytes)
    {
       /* byte[] bit = new byte[4];
        bit[0] = bytes[3];
        bit[1] = bytes[2];
        bit[2] = bytes[1];
        bit[3] = bytes[0];
        float v = 0;
        if (bit.length == 4)
        {
            v = IEEE754.bytesToFloat(bit);
            v = (float)(Math.round(v*1000)/1000);
        }*/
        float v = 0;
        v = IEEE754.bytesToFloat(bytes);
       // v = (float)(Math.round(v*1000)/1000);
        return v;
    }

    /**
     * 信号强度
     * @param signalstrength
     * @return
     */
    private static String getSignalFromByte(int signalstrength)
    {
        String str = "";
        if (signalstrength < 4 || signalstrength == 99)
        {
            str = "<-107db";
        }
        else if (signalstrength < 10)
        {
            str = "<-93db,>-107db";
        }
        else if (signalstrength < 16)
        {
            str = "<-71db,>-93db";
        }
        else if (signalstrength < 22)
        {
            str = "<-69db,>-71db";
        }
        else if (signalstrength < 28)
        {
            str = "<-57db,>-69db";
        }
        else
        {
            str = ">=-57db";
        }
        return str;
    }











    //20250113新增大表91、92系列命令



    public static final byte[] DTU_TYPE2 = {0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f};

    public static byte[] getTerminalParamsInfoBytes(List<PresetCommandDataInfo> commandDatas, String terminalAddress) {
        byte[] bit = null;
        if (CollectionUtils.isNotEmpty(commandDatas)) {
            bit = new byte[commandDatas.size() + 20];
            bit[0] = 0x68;
            bit[1] = (byte) Integer.parseInt(String.valueOf((bit.length % 100)),16);
            bit[2] = (byte) Integer.parseInt(String.valueOf((bit.length / 100)),16);
            bit[3] = 0x71;
            bit[4] = (byte) 0x90;
            byte[] temp = getByte(null, Integer.parseInt(terminalAddress), 4);
            System.arraycopy(temp, 0, bit, 5, 4);
            bit[9] = (byte) 0x91;
            temp = getLongByte(null, Long.parseLong(LocalDateTime.now().format(DateTimeFormatter.ofPattern("ssmmHHddMMyy"))), 6);
            System.arraycopy(temp, 0, bit, 10, 6);
            bit[16] = 0x00;
            bit[17] = (byte) commandDatas.size();
            int i = 18;
            for (PresetCommandDataInfo item : commandDatas) {
                bit[i] = (byte) Integer.parseInt(item.getTag().substring(4));
                i++;
            }
            bit[i++] = checkSum(bit);
            bit[i] = 0x16;
        }
        return bit;
    }

    /**
     * 多参数写命令
     */
    public static byte[] setTerminalParamsInfoBytes(List<PresetCommandDataInfo> commandDatas, String terminalAddress) {
        byte[] bit;
        byte[] tagDatas = new byte[0];
        if (CollectionUtils.isNotEmpty(commandDatas)) {
            for (PresetCommandDataInfo item : commandDatas) {
                byte[] tagData = getWriteByteByTag(item.getTag(), item.getContent());
                byte[] toolArray = new byte[tagData.length + tagDatas.length];
                System.arraycopy(tagDatas, 0, toolArray, 0, tagDatas.length);
                System.arraycopy(tagData, 0, toolArray, tagDatas.length, tagData.length);
                tagDatas = toolArray;
            }
        }
        bit = new byte[20 + tagDatas.length];
        bit[0] = 0x68;
        bit[1] = (byte) Integer.parseInt(String.valueOf((bit.length % 100)),16);
        bit[2] = (byte) Integer.parseInt(String.valueOf((bit.length / 100)),16);
        bit[3] = 0x71;
        bit[4] = (byte) 0x90;
        byte[] temp = getByte(null, Integer.parseInt(terminalAddress), 4);
        System.arraycopy(temp, 0, bit, 5, 4);
        bit[9] = (byte) 0x91;
        temp = getLongByte(null, Long.parseLong(LocalDateTime.now().format(DateTimeFormatter.ofPattern("ssmmHHddMMyy"))), 6);
        System.arraycopy(temp, 0, bit, 10, 6);
        bit[16] = 0x01;
        bit[17] = (byte) commandDatas.size();
        System.arraycopy(tagDatas, 0, bit, 18, tagDatas.length);
        bit[tagDatas.length + 18] = checkSum(bit);
        bit[tagDatas.length + 19] = 0x16;
        return bit;
    }

    /**
     * 初步解析软件客户端发过来的数据
     */
    // public static boolean getCsForAllType(byte[] bit, int index, byte[] type) {
    //     boolean flag = true;
    //     if (bit != null && bit.length > 0) {
    //         int receivedLen = bit[1] & 0xFF;
    //         int receivedLen2 = bit[2] & 0xFF;
    //         receivedLen = receivedLen2 * 100 + receivedLen;
    //         if (type.length > 0) {
    //             boolean temp = false;
    //             for (byte b : type) {
    //                 if (bit[index] == b) {
    //                     temp = true;
    //                     break;
    //                 }
    //             }
    //             flag = temp;
    //         }
    //         if (flag) {
    //             if (bit[0] != 104) {
    //                 flag = false;
    //             } else if (receivedLen != bit.length) {
    //                 flag = false;
    //             } else if (bit[(bit.length - 1)] != 22) {
    //                 flag = false;
    //             } else if (bit[3] != 114) {
    //                 flag = false;
    //             } else {
    //                 byte[] temp1 = new byte[bit.length - 2];
    //                 System.arraycopy(bit, 0, temp1, 0, temp1.length);
    //                 byte temp = checkSum(temp1);
    //                 if (temp != bit[(bit.length - 2)]) {
    //                     flag = false;
    //                 }
    //             }
    //         }
    //
    //
    //     }
    //
    //     return flag;
    // }


    /**
     * @param mark
     * @param ad
     * @param ws
     * @return
     */
    public static byte[] getByte(String mark, int ad, int ws) {
        byte[] bit = new byte[4];
        StringBuilder adTemp = new StringBuilder(Integer.toString(ad));
        int length = ws * 2 - adTemp.length();

        // 左侧填充零
        for (int i = 0; i < length; i++) {
            adTemp.insert(0, "0");
        }

        // 如果标记不为 null，则将其转换为字节并存储在 bit[0] 中
        if (mark != null) {
            bit[0] = (byte) Integer.parseInt(mark, 16);
        }

        // 解析地址并填充字节数组
        for (int i = 0; i < ws; i++) {
            int index = i * 2;
            String temp = adTemp.substring(index, index + 2);
            if (mark != null) {
                bit[i + 1] = (byte) Integer.parseInt(temp, 16);
            } else {
                bit[i] = (byte) Integer.parseInt(temp, 16);
            }
        }

        return bit;
    }

    /**
     * 校验和
     */
    private static byte checkSum(byte[] bit) {
        int sum = 0;
        if (bit.length > 0) {
            for (byte b : bit) {
                sum = sum + b;
                sum = sum & 0xFF;
            }
        }
        return (byte) sum;
    }

    public static byte[] getLongByte(String mark, long ad, int ws) {
        byte[] bit = new byte[ws];
        StringBuilder adTemp = new StringBuilder(String.valueOf(ad));
        int length = ws * 2 - adTemp.length();
        for (int i = 0; i < length; i++) {
            adTemp.insert(0, "0");
        }
        if (mark != null) {
            bit[0] = (byte) Integer.parseInt(mark, 16);
        }
        for (int i = 0; i < ws; i++) {
            int index = i * 2;
            String temp = adTemp.substring(index, index + 2);
            if (mark != null) {
                bit[i + 1] = (byte) Integer.parseInt(temp, 16);
            } else {
                bit[i] = (byte) Integer.parseInt(temp, 16);
            }

        }

        return bit;
    }

    public static byte[] getWriteByteByTag(String tag, Object data) {
        byte[] bit = null;
        if (tag != null) {
            switch (tag) {
                case "TAG_1":
                    bit = new byte[13];
                    bit[0] = 0x01;
                    byte[] tag1 = hexStringToByteArray(data.toString(), 12);
                    System.arraycopy(tag1, 0, bit, 1, 12);
                    break;
                case "TAG_2":
                    bit = new byte[13];
                    bit[0] = 0x02;
                    byte[] tag2 = hexStringToByteArray(data.toString(), 12);
                    System.arraycopy(tag2, 0, bit, 1, 12);
                    break;
                case "TAG_3":
                    bit = asciiStringToFixedLengthByteArray(data.toString(), 21, (byte) 3);
                    break;
                case "TAG_4":
                    bit = new byte[2];
                    bit[0] = 0x04;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                case "TAG_5":
                    bit = new byte[2];
                    bit[0] = 0x05;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                case "TAG_6":
                    bit = new byte[35];
                    String[] datas = data.toString().split(",");
                    String ip = datas[1].split(":")[1];
                    short port = Short.parseShort(datas[3].split(":")[1]);
                    byte ipType = (byte) Integer.parseInt(datas[2].split(":")[1]);
                    byte[] portBit = shortToByteArray(port);
                    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                        reverse(portBit);
                    }
                    byte[] ipBit = asciiStringToFixedLengthByteArray(ip, 31, (byte) 0);
                    bit[0] = 0x06;
                    bit[1] = (byte) Integer.parseInt(datas[0].split(":")[1]);
                    System.arraycopy(ipBit, 1, bit, 2, 30);
                    bit[32] = portBit[0];
                    bit[33] = portBit[1];
                    bit[34] = ipType;
                    break;
                case "TAG_7":
                    bit = new byte[3];
                    bit[0] = 0x07;
                    byte[] temp1 = shortToByteArray(Short.parseShort(data.toString()));
                    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                        reverse(temp1);
                    }
                    System.arraycopy(temp1, 0, bit, 1, 2);
                    break;
                case "TAG_8":
                    bit = new byte[3];
                    bit[0] = 0x08;
                    byte[] temp2 = shortToByteArray(Short.parseShort(data.toString()));
                    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                        reverse(temp2);
                    }
                    System.arraycopy(temp2, 0, bit, 1, 2);
                    break;
                case "TAG_9":
                    bit = new byte[2];
                    bit[0] = 0x09;
                    bit[1] = 0x01;
                    break;
                case "TAG_10":
                    bit = new byte[2];
                    bit[0] = 0x0A;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                case "TAG_11":
                    bit = asciiStringToFixedLengthByteArray(data.toString(), 31, (byte) 11);
                    break;
                case "TAG_12":
                    bit = new byte[2];
                    bit[0] = 0x0C;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                case "TAG_13":
                    bit = new byte[2];
                    bit[0] = 0x0D;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                case "TAG_14":
                    bit = new byte[2];
                    bit[0] = 0x0E;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                case "TAG_15":
                    bit = new byte[9];
                    bit[0] = 0x0F;
                    byte[] startTime = getLongByte(null, Long.parseLong(data.toString().substring(0, 8)), 4);
                    byte[] endTime = getLongByte(null, Long.parseLong(data.toString().substring(8, 16)), 4);
                    System.arraycopy(startTime, 0, bit, 1, 4);
                    System.arraycopy(endTime, 0, bit, 5, 4);
                    break;
                case "TAG_16":
                    bit = new byte[50];
                    String[] tag16Datas = data.toString().split(",");
                    String fileName = tag16Datas[0].split(":")[1];
                    int lengthBefore = Integer.parseInt(tag16Datas[1].split(":")[1]);
                    int lengthAfter = Integer.parseInt(tag16Datas[2].split(":")[1]);
                    byte checksum = (byte) Integer.parseInt(tag16Datas[3].split(":")[1]);
                    byte[] fileNameBit = asciiStringToFixedLengthByteArray(fileName, 41, (byte) 16);
                    byte[] lengthBeforeBit = intToByteArray(lengthBefore);
                    byte[] lengthAfterBit = intToByteArray(lengthAfter);
                    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                        reverse(lengthBeforeBit);
                        reverse(lengthAfterBit);
                    }
                    System.arraycopy(fileNameBit, 0, bit, 0, 41);
                    System.arraycopy(lengthBeforeBit, 0, bit, 41, 4);
                    System.arraycopy(lengthAfterBit, 0, bit, 45, 4);
                    bit[49] = checksum;
                    break;
                case "TAG_17":
                    bit = new byte[2];
                    bit[0] = 0x11;
                    bit[1] = 0x01;
                    break;
                case "TAG_18":
                    bit = new byte[2];
                    bit[0] = 0x12;
                    bit[1] = (byte) Integer.parseInt(data.toString());
                    break;
                default:
                    bit = new byte[0];
                    break;
            }
        }
        return bit;
    }


    /**
     * 16进制字符转byte[]不足length补零
     *
     * @param hexString     16进制字符串
     * @param desiredLength byte[]长度
     * @return byte[]
     */
    public static byte[] hexStringToByteArray(String hexString, int desiredLength) {
        byte[] bit = new byte[desiredLength];
        hexString = hexString.replaceAll(" ", "");
        if ((hexString.length() % 2) != 0) {
            hexString = "0" + hexString;
        }
        byte[] returnBytes = new byte[hexString.length() / 2];
        for (int i = 0; i < returnBytes.length; i++) {
            returnBytes[i] = (byte) Integer.parseInt(hexString.substring(i * 2, i * 2 + 2), 16);
        }

        System.arraycopy(returnBytes, 0, bit, 0, returnBytes.length);
        return bit;
    }

    /**
     * ascii转byte[]不足length补零并设置首位
     *
     * @param asciiString ascii
     * @param fixedLength byte[]长度
     * @param firstByte   首位标志位
     * @return byte[]
     */
    public static byte[] asciiStringToFixedLengthByteArray(String asciiString, int fixedLength, byte firstByte) {
        byte[] byteArray = new byte[fixedLength];
        // 设置首位标志位
        byteArray[0] = firstByte;
        char[] asciiStringArray = asciiString.toCharArray();
        for (int i = 1; i < asciiString.length() + 1 && i < fixedLength; i++) {
            byteArray[i] = (byte) asciiStringArray[i - 1];
        }
        return byteArray;
    }

    /**
     * short转字节数组（返回小端字节序）
     *
     * @param value short
     * @return 字节数组
     */
    public static byte[] shortToByteArray(short value) {
        return new byte[]{
                // 低位字节
                (byte) (value & 0xFF),
                // 高位字节
                (byte) ((value >> 8) & 0xFF)
        };
    }

    /**
     * int转字节数组（返回小端字节序）
     *
     * @param value int
     * @return 字节数组
     */
    public static byte[] intToByteArray(int value) {
        return new byte[]{
                // 低位字节
                (byte) (value & 0xFF),
                (byte) ((value >> 8) & 0xFF),
                (byte) ((value >> 16) & 0xFF),
                // 高位字节
                (byte) ((value >> 24) & 0xFF)
        };
    }

    /**
     * 多参数读取递归解析
     */
    public static List<PresetCommandDataInfo> recursiveParseReadParam(byte[] bit, List<PresetCommandDataInfo> list) {
        PresetCommandDataInfo presetCommandData = new PresetCommandDataInfo();
        String tag = "";
        Object content = "";
        switch (bit[0]) {
            case 1:
                byte[] tag1 = new byte[12];
                System.arraycopy(bit, 1, tag1, 0, 12);
                tag = "TAG_1";
                content = Hex.encodeHexString(tag1);
                bit = Arrays.copyOfRange(bit, 13, bit.length);
                break;
            case 2:
                byte[] tag2 = new byte[12];
                System.arraycopy(bit, 1, tag2, 0, 12);
                tag = "TAG_2";
                content = Hex.encodeHexString(tag2);
                bit = Arrays.copyOfRange(bit, 13, bit.length);
                break;
            case 3:
                byte[] tag3 = new byte[20];
                System.arraycopy(bit, 1, tag3, 0, 20);
                tag = "TAG_3";
                content = new String(tag3, StandardCharsets.US_ASCII).replace("\u0000", "");
                bit = Arrays.copyOfRange(bit, 21, bit.length);
                break;
            case 4:
                tag = "TAG_4";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            case 5:
                tag = "TAG_5";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            case 6:
                byte[] ipBit = new byte[30];
                byte[] portBit = new byte[2];
                System.arraycopy(bit, 2, ipBit, 0, 30);
                System.arraycopy(bit, 32, portBit, 0, 2);
                String ip = new String(ipBit, StandardCharsets.US_ASCII).replace("\u0000", "");
                // 检查当前系统的字节序（Java 默认使用大端字节序）
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                    // 如果是小端序，反转字节数组
                    reverse(portBit);
                }
                // 将字节数组转换为短整数（16 位）
                short port = ByteBuffer.wrap(portBit).getShort();
                tag = "TAG_6";
                content = "中心号:" + (bit[1] == 0 ? "主中心" : "副中心") + ",IP地址:" + ip + ",IP类型:" + (bit[34] == 0 ? "TCP" : "UDP") + ",端口号:" + port;
                bit = Arrays.copyOfRange(bit, 35, bit.length);
                break;
            case 7:
                byte[] tag7 = new byte[2];
                tag = "TAG_7";
                System.arraycopy(bit, 1, tag7, 0, 2);
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                    reverse(tag7);
                }
                content = ByteBuffer.wrap(tag7).getShort();
                bit = Arrays.copyOfRange(bit, 3, bit.length);
                break;
            case 8:
                byte[] tag8 = new byte[2];
                tag = "TAG_8";
                System.arraycopy(bit, 1, tag8, 0, 2);
                if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
                    reverse(tag8);
                }
                content = ByteBuffer.wrap(tag8).getShort();
                bit = Arrays.copyOfRange(bit, 3, bit.length);
                break;
            case 10:
                tag = "TAG_10";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            case 11:
                byte[] tag11 = new byte[30];
                System.arraycopy(bit, 1, tag11, 0, 30);
                tag = "TAG_11";
                content = new String(tag11, StandardCharsets.US_ASCII).replace("\u0000", "");
                bit = Arrays.copyOfRange(bit, 31, bit.length);
                break;
            case 12:
                tag = "TAG_12";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            case 13:
                tag = "TAG_13";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            case 14:
                tag = "TAG_14";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            case 18:
                tag = "TAG_18";
                content = bit[1];
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                break;
            default:
                break;
        }
        presetCommandData.setTag(tag);
        presetCommandData.setContent(content);
        list.add(presetCommandData);
        if (bit.length == 0) {
            return list;
        }
        return recursiveParseReadParam(bit, list);
    }

    /**
     * 反转字节数组的方法
     */
    private static void reverse(byte[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            byte temp = array[left];
            array[left] = array[right];
            array[right] = temp;
            left++;
            right--;
        }
    }


    public static byte[] getTerminalDsDateInfoBytes(String oldAddr, String date) {
        byte[] bit = null;
        if (date.length() == 4) {
            date = Year.now().format(DateTimeFormatter.ofPattern("yy")) + date;
        }
        if (StringUtils.isNotBlank(oldAddr)) {
            // 68 16 00 71 20 00 00 00 01 21 00 00 00 02 33 16
            bit = new byte[15];
            bit[0] = 0x68;
            bit[1] = 0x15;
            bit[2] = 0;
            bit[3] = 0x71;
            bit[4] = DTU_TYPE2[0];
            byte[] temp = getByte(null, Integer.parseInt(oldAddr), 4);
            System.arraycopy(temp, 0, bit, 5, 4);
            bit[9] = DTU_TYPE2[15];
            // 日期
            temp = getByte(null, Integer.parseInt(date), 3);
            System.arraycopy(temp, 0, bit, 10, 3);
            bit[13] = checkSum(bit);
            bit[14] = 0x16;
        }
        return bit;
    }


    public static List<PresetCommandDataInfo> parseReadParam(byte[] data) {
        List<PresetCommandDataInfo> list = new ArrayList<>();
        if (data.length > 0) {
            list = recursiveParseReadParam(data, list);
        }
        return list;
    }



    public static JSONObject getBufferPacketForTerminalData(byte[] bit)
    {
        JSONObject jo = new JSONObject();
        byte type = (byte) 0x92;
        boolean flag = getCsForTerminalData(bit, 9, type);
        if (flag)
        {
            try
            {
                int tempLength = bit.length - 13;
                byte[] bitRes = new byte[tempLength];
                System.arraycopy(bit, 11, bitRes, 0, tempLength);
                jo = recursivePars(bitRes, jo);
                return jo;
            }catch(Exception e) { return null; }
        }
        return null;
    }

    public static boolean getCsForTerminalData(byte[] bit, int index, byte type)
    {
        boolean flag = true;
        if(bit != null)
        {
            int recvlen = bit[1] & 0xFF;
            int recvlen2 = bit[2] & 0xFF;
            recvlen = recvlen2 * 100 + recvlen;
            if (bit[0] != 104)
            {
                flag = false;
            }else if(recvlen != bit.length)
            {
                flag = false;
            }else if(bit[bit.length - 1] != 22)
            {
                flag = false;
            }else if (bit[index] != type)
            {
                flag = false;
            }else if(bit[3] != 114)
            {
                flag = false;
            }
            else
            {
                byte[] temp1 = new byte[bit.length - 2];
                System.arraycopy(bit, 0, temp1, 0, temp1.length);
                byte temp = getTotalForByte(temp1);
                if (temp != bit[bit.length - 2])
                {
                    flag = false;
                }
            }
        }
        return flag;
    }


    public static JSONObject recursivePars(byte[] bit, JSONObject jo)
    {

        switch (bit[0])
        {
            //出厂编号
            case 1:
                byte[] bitFactoryCode = new byte[15];
                System.arraycopy(bit, 1, bitFactoryCode, 0, 15);
                bit = Arrays.copyOfRange(bit, 16, bit.length);
                String factoryCode = new String(bitFactoryCode,StandardCharsets.US_ASCII).replace("\u0000", "");
                jo.put("TerminalFactoryID", factoryCode);
                break;
            //终端当前时间
            case 2:
                byte[] bitTerminalTime = new byte[6];
                System.arraycopy(bit, 1, bitTerminalTime, 0, 6);
                bit = Arrays.copyOfRange(bit, 7, bit.length);
                String terminalTime = getStringBCDFromByteFHigh(bitTerminalTime);
                terminalTime = Year.now().toString().substring(0, 2) + terminalTime;
                jo.put("TerminalTime", terminalTime);
                break;
            //硬件类别
            case 3:
                byte[] bitHardwareType = new byte[20];
                System.arraycopy(bit, 1, bitHardwareType, 0, 20);
                bit = Arrays.copyOfRange(bit, 21, bit.length);
                String hardwareType = new String(bitHardwareType, StandardCharsets.US_ASCII).replace("\u0000", "");
                jo.put("CateGory", hardwareType);
                break;
            //软件版本号
            case 4:
                byte[] bitSoftwareType = new byte[20];
                System.arraycopy(bit, 1, bitSoftwareType, 0, 20);
                bit = Arrays.copyOfRange(bit, 21, bit.length);
                String softwareType = new String(bitSoftwareType,StandardCharsets.US_ASCII).replace("\u0000", "");
                jo.put("Version", softwareType);
                break;
            //IMEI
            case 5:
                byte[] bitImei = new byte[15];
                System.arraycopy(bit, 1, bitImei, 0, 15);
                bit = Arrays.copyOfRange(bit, 16, bit.length);
                String imei = new String(bitImei,StandardCharsets.US_ASCII).replace("\u0000", "");
                jo.put("IMEI", imei);
                break;
            //ICCID
            case 6:
                byte[] bitIccid = new byte[20];
                System.arraycopy(bit, 1, bitIccid, 0, 20);
                bit = Arrays.copyOfRange(bit, 21, bit.length);
                String iccid = new String(bitIccid,StandardCharsets.US_ASCII).replace("\u0000", "");
                jo.put("ICCID", iccid);
                break;
            //IMSI
            case 7:
                byte[] bitImsi = new byte[20];
                System.arraycopy(bit, 1, bitImsi, 0, 20);
                bit = Arrays.copyOfRange(bit, 21, bit.length);
                String imsi = new String(bitImsi,StandardCharsets.US_ASCII).replace("\u0000", "");
                jo.put("IMSI", imsi);
                break;
            //CSQ
            case 8:
                byte[] csq = new byte[1];
                System.arraycopy(bit, 1, csq, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("CSQ", csq[0]);
                break;
            //当前正向累计
            case 9:
                byte[] bitReadNumber = new byte[4];
                System.arraycopy(bit, 1, bitReadNumber, 0, 4);
                bit = Arrays.copyOfRange(bit, 5, bit.length);
                float readNumber = getFloatFromByte(bitReadNumber);
                jo.put("ReadNumber", readNumber);
                break;
            //当前反向累计
            case 10:
                byte[] bitReverseNumber = new byte[4];
                System.arraycopy(bit, 1, bitReverseNumber, 0, 4);
                bit = Arrays.copyOfRange(bit, 5, bit.length);
                float reverseNumber = getFloatFromByte(bitReverseNumber);
                jo.put("ReverseNumber", reverseNumber);
                break;
            //当前净向累计
            case 11:
                byte[] bitTotalFlow = new byte[4];
                System.arraycopy(bit, 1, bitTotalFlow, 0, 4);
                bit = Arrays.copyOfRange(bit, 5, bit.length);
                float totalFlow = getFloatFromByte(bitTotalFlow);
                jo.put("TotalFlow", totalFlow);
                break;
            //当前瞬时
            case 12:
                byte[] bitInstantNumber = new byte[4];
                System.arraycopy(bit, 1, bitInstantNumber, 0, 4);
                bit = Arrays.copyOfRange(bit, 5, bit.length);
                float instantNumber = getFloatFromByte(bitInstantNumber);
                jo.put("InstantNumber", instantNumber);
                break;
            //当前压力
            case 13:
                byte[] bitPressure = new byte[4];
                System.arraycopy(bit, 1, bitPressure, 0, 4);
                bit = Arrays.copyOfRange(bit, 5, bit.length);
                float pressure = getFloatFromByte(bitPressure);
                jo.put("Pressure", pressure);
                break;
            //终端电压
            case 14:
                byte[] bitTerminalVol = new byte[2];
                System.arraycopy(bit, 1, bitTerminalVol, 0, 2);
                bit = Arrays.copyOfRange(bit, 3, bit.length);
                String terminalVol = String.valueOf(((double) Convert.bytesToInt(bitTerminalVol) / 100));
                jo.put("TerminalVol", terminalVol);
                break;
            //计量电压
            case 15:
                byte[] bitMeteringVol = new byte[2];
                System.arraycopy(bit, 1, bitMeteringVol, 0, 2);
                bit = Arrays.copyOfRange(bit, 3, bit.length);
                String meteringVol = String.valueOf(((double) Convert.bytesToInt(bitMeteringVol) / 100));
                jo.put("MeteringVol", meteringVol);
                break;
            //采集周期
            case 16:
                byte[] bitAcquisitionCycle = new byte[2];
                System.arraycopy(bit, 1, bitAcquisitionCycle, 0, 2);
                bit = Arrays.copyOfRange(bit, 3, bit.length);
                int acquisitionCycle = Convert.bytesToInt(bitAcquisitionCycle);
                jo.put("Acquisitioncycle", acquisitionCycle);
                break;
            //上报周期
            case 17:
                byte[] bitEscalationCycle = new byte[2];
                System.arraycopy(bit, 1, bitEscalationCycle, 0, 2);
                bit = Arrays.copyOfRange(bit, 3, bit.length);
                int escalationCycle = Convert.bytesToInt(bitEscalationCycle);
                jo.put("Reportingcycle", escalationCycle);
                break;
            //SIM卡运营商
            case 18:
                byte[] simOperators = new byte[1];
                System.arraycopy(bit, 1, simOperators, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("SIMOperator", simOperators[0]);
                break;
            //SIM卡类别
            case 19:
                byte[] bitSimType = new byte[1];
                System.arraycopy(bit, 1, bitSimType, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("SIMCateGory", bitSimType[0]);
                break;
            //主中心协议号
            case 20:
                byte[] mainProtocol = new byte[1];
                System.arraycopy(bit, 1, mainProtocol, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("MainCenter", mainProtocol[0]);
                break;
            //副中心协议号
            case 21:
                byte[] deputyProtocol = new byte[1];
                System.arraycopy(bit, 1, deputyProtocol, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("SubCenter", deputyProtocol[0]);
                break;
            //主中心IP信息
            case 22:
                byte[] bitMainIp = new byte[34];
                System.arraycopy(bit, 1, bitMainIp, 0, 34);
                bit = Arrays.copyOfRange(bit, 35, bit.length);
                String mainIp = new String(Arrays.copyOfRange(bitMainIp, 1, 31),StandardCharsets.US_ASCII).replace("\u0000", "");
                String mainPort = String.valueOf(Convert.toShort(Arrays.copyOfRange(bitMainIp, 31, 33)));
                String mainType = String.valueOf(bitMainIp[bitMainIp.length - 1]);
                jo.put("MainIP", mainIp);
                jo.put("MainPort", mainPort);
                jo.put("MainType", mainType);
                break;
            //副中心IP信息
            case 23:
                byte[] deputyIp = new byte[34];
                System.arraycopy(bit, 1, deputyIp, 0, 34);
                bit = Arrays.copyOfRange(bit, 35, bit.length);
                String subIp = new String(Arrays.copyOfRange(deputyIp, 1, 31),StandardCharsets.US_ASCII).replace("\u0000", "");
                String subPort = String.valueOf(Convert.toShort(Arrays.copyOfRange(deputyIp, 31, 33)));
                String subType = String.valueOf(deputyIp[deputyIp.length - 1]);
                jo.put("SubIP", subIp);
                jo.put("SubPort", subPort);
                jo.put("SubType", subType);
                break;
            //报警信息
            case 24:
                byte[] warnInfo = new byte[24];
                System.arraycopy(bit, 1, warnInfo, 0, 24);
                bit = Arrays.copyOfRange(bit, 25, bit.length);
                break;
            //升级状态信息
            case 25:
                byte[] bitUpgradeState = new byte[1];
                System.arraycopy(bit, 1, bitUpgradeState, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("State", bitUpgradeState[0]);
                break;
            //阀门状态信息
            case 26:
                byte[] bitValve = new byte[1];
                System.arraycopy(bit, 1, bitValve, 0, 1);
                bit = Arrays.copyOfRange(bit, 2, bit.length);
                jo.put("Valve", bitValve[0]);
                break;
            default:
                break;
        }
        if(bit.length == 0)
        {
            return jo;
        }
        return recursivePars(bit,jo);
    }
}
